package com.xcw.bos.web.action;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

import org.apache.commons.lang3.StringUtils;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.xcw.bos.domain.Region;
import com.xcw.bos.utils.PinYin4jUtils;
import com.xcw.bos.web.action.base.BaseAction;
/**
 * 区域管理Action
 * @author xcw
 *
 */
@Controller
@Scope("prototype")
public class RegionAction extends BaseAction<Region> {

	private File upload;//用于接收上传到文件
	
	public void setUpload(File upload){
		this.upload = upload;
	}
	
	//导入区域数据
	public String importXls() throws FileNotFoundException, IOException{
		System.out.println(upload);
		//加载Excel文件到内存中
		HSSFWorkbook workbook = new HSSFWorkbook(new FileInputStream(upload));
		//读取第一个Sheet页
		HSSFSheet sheet = workbook.getSheetAt(0);
		
		List<Region> list = new ArrayList<Region>();
		for(Row row : sheet){
			String value1 = row.getCell(0).getStringCellValue();//id
			String value2 = row.getCell(1).getStringCellValue();//provide
			String value3 = row.getCell(2).getStringCellValue();// city
			String value4 = row.getCell(3).getStringCellValue();// district
			String value5 = row.getCell(4).getStringCellValue();// postcode
			Region region = new Region(value1, value2, value3, value4, value5);
			
			//简码
			String shortcode = StringUtils.join(
					PinYin4jUtils.getHeadByString(value2 + value3), "");
			//城市编码
			String citycode = PinYin4jUtils.hanziToPinyin(value3, "");
			region.setShortcode(shortcode);
			region.setCitycode(citycode);
			list.add(region);
		}
		String f = "1";
		try {
			regionService.saveBatch(list);
		} catch (Exception e) {
			f = "0";
		}
		
		ServletActionContext.getResponse().setContentType("text/html;charset=UTF-8");
		ServletActionContext.getResponse().getWriter().print(f);
		return NONE;
	}
	
	//分页查询
	@RequiresPermissions("region-query")//执行这个方法，需要具有region-query权限
	@RequiresRoles("admin")//执行这个方法需要具有admin角色
	public String pageQuery() throws IOException{
		regionService.pageQuery(pageBean);
		//将PageBean对象序列化为json数据
		//排除不需要序列化的属性
		JsonConfig jc = new JsonConfig();
		jc.setExcludes(new String[]{"page", "pageSize", "detachedCriteria",
		"subareas" });
		
		JSONObject jsonObject = JSONObject.fromObject(pageBean, jc);
		String json = jsonObject.toString();
		
		ServletActionContext.getResponse().setContentType("text/json;charset=UTF-8");
        ServletActionContext.getResponse().getWriter().print(json);
        return NONE;
	}
	
	private String q;
	
	public void setQ(String q){
		if(StringUtils.isNotBlank(q)){
			this.q = q.trim();
		}
	}
	
	//查询所有区域数据，返回json
	public String listajax() throws IOException{
		List<Region> list = null;
		if(StringUtils.isBlank(q)){
			list = regionService.findAll();
		}else{
			list = regionService.findByQ(q);
		}
		JsonConfig jc = new JsonConfig();
		jc.setExcludes(new String[]{"subareas"});
		
		JSONArray jsonObject = JSONArray.fromObject(list, jc);
		String json = jsonObject.toString();
		
		ServletActionContext.getResponse().setContentType("text/json;charset=UTF-8");
		ServletActionContext.getResponse().getWriter().print(json);
		return NONE;
		}
	}

